#This script replicates all tables and figures in the main manuscript for "Participatory Unilateralism"
rm(list = ls())

#Load data
load("replication_data.Rdata")

#Load packages
pacman::p_load(tidyverse, modelsummary, fixest, MASS, kableExtra, ggplot2)



#Table 2: Baseline Specifications, Full Scale ----
#Any Hearing
s1 <- glm(any.hearing ~ Delegation*Discretion, data = eo.df, family = 'binomial')
s2 <- feglm(any.hearing ~ Delegation*Discretion | president, data = eo.df, family = 'binomial')
s3 <- feglm(any.hearing ~ Delegation*Discretion | year, data = eo.df, family = 'binomial')
s4 <- feglm(any.hearing ~ Delegation*Discretion | majortopic, data = eo.df, family = 'binomial')

#Total Hearings
s5 <- glm.nb(n.hearings ~ Delegation*Discretion, data = eo.df, control = glm.control(maxit = 100))
s6 <- fenegbin(n.hearings ~ Delegation*Discretion | president, data = eo.df)
s7 <- fenegbin(n.hearings ~ Delegation*Discretion | year, data = eo.df)
s8 <- fenegbin(n.hearings ~ Delegation*Discretion | majortopic, data = eo.df)


#Table
fe.row <- data.frame(rbind(c("FE", "None", "President", "Year", "Policy", "None", "President", "Year", "Policy")))

modelsummary(list(s1, s2, s3, s4, s5, s6, s7, s8),
             stars = TRUE,
             output = 'kableExtra',
             coef_omit = "president*|year*|majortopic*",
             coef_map = c("Delegation" = "Delegation", "Discretion" = "Discretion", "Delegation:Discretion" = "Del. x Dis.", "(Intercept)" = "(Intercept)"),
             gof_map = "nobs",
             add_rows = fe.row,
             title = "Baseline Specifications, Full Scale")   %>%
    kableExtra::add_header_above(c(" " = 1, "Any Hearing (Logit)" = 4, "Total Hearings (Neg. Binomial)" = 4)) %>%
    kableExtra::save_kable("./tablesi2.png")


#Table 3: Full Specifications, Full Scale ----
#Any Hearing
f1 <- glm(any.hearing ~ Delegation*Discretion + eo.age + previous.hearings+ chamber + divided.gov, data = eocongress.df, family = 'binomial')
f2 <- feglm(any.hearing ~ Delegation*Discretion + eo.age + previous.hearings+ chamber + divided.gov | president, data = eocongress.df, family = 'binomial')
f3 <- feglm(any.hearing ~ Delegation*Discretion + eo.age + previous.hearings+ chamber + divided.gov | year, data = eocongress.df, family = 'binomial')
f4 <- feglm(any.hearing ~ Delegation*Discretion + eo.age + previous.hearings+ chamber + divided.gov | majortopic, data = eocongress.df, family = 'binomial')

#Total Hearings
f5 <- glm.nb(n.hearings ~ Delegation*Discretion + eo.age + previous.hearings+ chamber + divided.gov, data = eocongress.df, control = glm.control(maxit = 100))
f6 <- fenegbin(n.hearings ~ Delegation*Discretion + eo.age+ previous.hearings + chamber + divided.gov | president, data = eocongress.df)
f7 <- fenegbin(n.hearings ~ Delegation*Discretion + eo.age + previous.hearings+ chamber + divided.gov | year, data = eocongress.df)
f8 <- fenegbin(n.hearings ~ Delegation*Discretion + eo.age + previous.hearings+ chamber + divided.gov | majortopic, data = eocongress.df)


#Table
modelsummary(list(f1, f2, f3, f4, f5, f6, f7, f8),
             stars = TRUE,
             output = 'kableExtra',
             coef_omit = "president*|year*|majortopic*",
             coef_map = c("Delegation" = "Delegation", "Discretion" = "Discretion", "Delegation:Discretion" = "Del. x Dis.", "eo.age" = "EO Age", "previous.hearings" = "Hearings", "chamberSenate" = "Senate", "divided.gov" = "Divided", "(Intercept)" =
                              "(Intercept)"),
             gof_map = "nobs",
             add_rows = fe.row,
             title = "Full Specifications, Full Scale")  %>%
    kableExtra::add_header_above(c(" " = 1, "Any Hearing (Logit)" = 4, "Total Hearings (Neg. Binomial)" = 4)) %>%
    kableExtra::save_kable("./tablesi3.png")



#Table 4: Baseline Specifications, No Interaction ----
#Any Hearing
si.b1 <- glm(any.hearing ~ hidel + hidis, data = eo.df, family = 'binomial')
si.b2 <- feglm(any.hearing ~ hidel + hidis | president, data = eo.df, family = 'binomial')
si.b3 <- feglm(any.hearing ~ hidel + hidis | year, data = eo.df, family = 'binomial')
si.b4 <- feglm(any.hearing ~ hidel + hidis | majortopic, data = eo.df, family = 'binomial')

#Total Hearings
si.b5 <- glm.nb(n.hearings ~ hidel + hidis, data = eo.df, control = glm.control(maxit = 100))
si.b6 <- fenegbin(n.hearings ~ hidel + hidis | president, data = eo.df)
si.b7 <- fenegbin(n.hearings ~ hidel + hidis | year, data = eo.df)
si.b8 <- fenegbin(n.hearings ~ hidel + hidis | majortopic, data = eo.df)

#Table
fe.row <- data.frame(rbind(c("FE", "None", "President", "Year", "Policy", "None", "President", "Year", "Policy")))

modelsummary(list(si.b1, si.b2, si.b3, si.b4, si.b5, si.b6, si.b7, si.b8),
             stars = TRUE,
             output = 'kableExtra',
             coef_omit = "president*|year*|majortopic*",
             coef_map = c("hidel" = "Delegation", "hidis" = "Discretion", "hidel:hidis" = "Del. x Dis.", "(Intercept)" = "(Intercept)"),
             gof_map = "nobs",
             add_rows = fe.row,
             title = "Baseline Specifications, Robustness to No Interaction")   %>%
    kableExtra::add_header_above(c(" " = 1, "Any Hearing (Logit)" = 4, "Total Hearings (Neg. Binomial)" = 4)) %>%
    kableExtra::save_kable("./tablesi4.png")


#Table 5: Full Specifications, No Interaction ----
#Any Hearing
si.f1 <- glm(any.hearing ~ hidel + hidis + eo.age + previous.hearings+ chamber + divided.gov, data = eocongress.df, family = 'binomial')
si.f2 <- feglm(any.hearing ~ hidel + hidis + eo.age + previous.hearings+ chamber + divided.gov | president, data = eocongress.df, family = 'binomial')
si.f3 <- feglm(any.hearing ~ hidel + hidis + eo.age + previous.hearings+ chamber + divided.gov | year, data = eocongress.df, family = 'binomial')
si.f4 <- feglm(any.hearing ~ hidel + hidis + eo.age + previous.hearings+ chamber + divided.gov | majortopic, data = eocongress.df, family = 'binomial')

#Total Hearings
si.f5 <- glm.nb(n.hearings ~ hidel + hidis + eo.age + previous.hearings+ chamber + divided.gov, data = eocongress.df)
si.f6 <- fenegbin(n.hearings ~ hidel + hidis + eo.age+ previous.hearings + chamber + divided.gov | president, data = eocongress.df)
si.f7 <- fenegbin(n.hearings ~ hidel + hidis + eo.age + previous.hearings+ chamber + divided.gov | year, data = eocongress.df)
si.f8 <- fenegbin(n.hearings ~ hidel + hidis + eo.age + previous.hearings+ chamber + divided.gov | majortopic, data = eocongress.df)


### Table
modelsummary(list(si.f1, si.f2, si.f3, si.f4, si.f5, si.f6, si.f7, si.f8),
             stars = TRUE,
             output = 'kableExtra',
             coef_omit = "president*|year*|majortopic*",
             coef_map = c("hidel" = "Delegation", "hidis" = "Discretion", "hidel:hidis" = "Del. x Dis.", "eo.age" = "EO Age", "previous.hearings" = "Hearings", "chamberSenate" = "Senate", "divided.gov" = "Divided", "(Intercept)" =
                              "(Intercept)"),
             gof_map = "nobs",
             add_rows = fe.row,
             title = "Full Specifications, No Interaction")  %>%
    kableExtra::add_header_above(c(" " = 1, "Any Hearing (Logit)" = 4, "Total Hearings (Neg. Binomial)" = 4)) %>%
    kableExtra::save_kable("./tablesi5.png")


#Figure 2: Distribution of EO Age among EOs Receiving Hearings ----

eocongress.df.dv <- eocongress.df[eocongress.df$any.hearing == 1,]
ggsave(ggplot(eocongress.df.dv, aes(x = eo.age)) +
           geom_histogram(binwidth = 1) +
           theme_bw() +
           labs(x = "EO Age", y = "Count") +
           geom_vline(aes(xintercept = mean(x = eo.age)), color = 'red'),
       filename = "./figsi2.png")

#Table 6: Full Specifications, EO Age 3 or less----

#Filter on EO Age 3 or less
eocongress.df3 <-
    eocongress.df %>%
    filter(eo.age <= 3)
    

#Any Hearing
si.f1.age <- glm(any.hearing ~ hidel*hidis + eo.age + previous.hearings+ chamber + divided.gov, data = eocongress.df3, family = 'binomial')
si.f2.age <- feglm(any.hearing ~ hidel*hidis + eo.age + previous.hearings+ chamber + divided.gov | president, data = eocongress.df3, family = 'binomial')
si.f3.age <- feglm(any.hearing ~ hidel*hidis + eo.age + previous.hearings+ chamber + divided.gov | year, data = eocongress.df3, family = 'binomial')
si.f4.age <- feglm(any.hearing ~ hidel*hidis + eo.age + previous.hearings+ chamber + divided.gov | majortopic, data = eocongress.df3, family = 'binomial')

#Total Hearings
si.f5.age <- glm.nb(n.hearings ~ hidel*hidis + eo.age + previous.hearings+ chamber + divided.gov, data = eocongress.df3)
si.f6.age <- fenegbin(n.hearings ~ hidel*hidis + eo.age+ previous.hearings + chamber + divided.gov | president, data = eocongress.df3)
si.f7.age <- fenegbin(n.hearings ~ hidel*hidis + eo.age + previous.hearings+ chamber + divided.gov | year, data = eocongress.df3)
si.f8.age <- fenegbin(n.hearings ~ hidel*hidis + eo.age + previous.hearings+ chamber + divided.gov | majortopic, data = eocongress.df3)


### Table
modelsummary(list(si.f1.age, si.f2.age, si.f3.age, si.f4.age, si.f5.age, si.f6.age, si.f7.age, si.f8.age),
             stars = TRUE,
             output = 'kableExtra',
             coef_omit = "president*|year*|majortopic*",
             coef_map = c("hidel" = "Delegation", "hidis" = "Discretion", "hidel:hidis" = "Del. x Dis.", "eo.age" = "EO Age", "previous.hearings" = "Hearings", "chamberSenate" = "Senate", "divided.gov" = "Divided", "(Intercept)" =
                              "(Intercept)"),
             gof_map = "nobs",
             add_rows = fe.row,
             title = "Full Specifications, EO Age 3 or less")  %>%
    kableExtra::add_header_above(c(" " = 1, "Any Hearing (Logit)" = 4, "Total Hearings (Neg. Binomial)" = 4)) %>%
    kableExtra::save_kable("./tablesi6.png")



